//index.js
const app = getApp();
import regeneratorRuntime from '../../utils/runtime'

Page({
  data: {
    AssTypeIdx: 0,
    index: {}, //首页数据
    AssIdx: 0, //协会产品的企业搜索
    pIdx: '0', //会员福利选择
    flag: true,
    navList: [{ 
        img: '/img/home-nav-1@2x.png',
        title: '关于商会'
      },
      {
        img: '/img/home-nav-2@2x.png',
        title: '组织架构'
      },
      {
        img: '/img/home-nav-2@2x.png',
        title: '商会章程'
      },
      {
        img: '/img/home-nav-2@2x.png',
        title: '协会简介'
      },
      {
        img: '/img/home-nav-5@2x.png',
        title: '申请加入'
      }, {
        img: '/img/home-nav-2@2x.png',
        title: '会员区域'
      },
      {
        img: '/img/home-nav-4@2x.png',
        title: '会员服务'
      }, {
        img: '/img/home-nav-2@2x.png',
        title: '商会公益'
      },
      {
        img: '/img/home-nav-3@2x.png',
        title: '商会活动'
      }
    ],
    newsList: [],
    productAll: []
  },

  click: function (e) {
    let idx = e.currentTarget.dataset.idx;
    var id = e.currentTarget.dataset.id;
    var that = this;
    var navurl = '';
    app.confirmLogin(
      function () {
        switch (idx) {
          case 0: //关于协会
          case '11': //协会介绍
            navurl = '/pages/about/about';
            break;
          case 1: //会员名录
            navurl = '/pages/framework/framework';
            break;
          case 5: //会员名录
            navurl = '/pages/company-list/company-list';
            break;
          case 8: //商会活动
            navurl = '/pages/activity-list/activity-list';
            break;
          case 2: //商会章程
            navurl = '/pages/rules/rules';
            break;
          case 3: //会长风采
            navurl = '/pages/businesstyle/businesstyle';
            break;
          case 6: //会员服务
            navurl = '/pages/product-list/product-list';
            break;
          case 7: //商会公益
            navurl = '/pages/msg-list/msg-list?is_special=1';
            break;
          case 4: //申请加入
            
            navurl = '/pages/join/join';
            break;
        }
          wx.navigateTo({
            url: navurl,
          })
      })
  },
  searchTap: function (e) {
    if (!e.detail.value) {
      return;
    }
    wx.navigateTo({
      url: '/pages/activity-list/activity-list?search=' + e.detail.value,
    })
  },
  openMsgDetail: function (e) { //资讯详情
    wx.navigateTo({
      url: '../msg-detail/msg-detail?id=' + e.currentTarget.dataset.id,
    })
  },


  openActiveDetail: function (e) { //打开活动详情
    let that = this;
    let id = e.currentTarget.dataset.id;
    app.confirmLogin(
      function () {
        wx.navigateTo({
          url: '/pages/activity-detail/activity-detail?id=' + id,
        })
      })
  },

  vipProductSelectTap: function (e) { //会员福利选择
    let that = this;
    let idx = e.currentTarget.dataset.idx;
    that.setData({
      pIdx: idx
    })
  },

  openAssociaDetail: function (e) { //协会产品详情
    let that = this;
    let id = e.currentTarget.dataset.id;
    app.confirmLogin(
      function () {
        wx.navigateTo({
          url: '/pages/associa-product/associa-product?id=' + id,
        })
      })
  },

  openPeopleCard: function (e) { //会员名录
    let id = e.currentTarget.dataset.id;
    app.confirmLogin(
      function () {
        if (id == wx.getStorageSync('userId')) {
          wx.navigateTo({
            url: '/pages/my-card/my-card',
          })
          return;
        }
        wx.navigateTo({
          url: '/pages/people-card/people-card?id=' + id,
        })
      })

  },


  lookBtnTap: function (e) { //查看会员企业详情
    var id = e.currentTarget.dataset.id;

    app.confirmLogin(
      function () {
        wx.navigateTo({
          url: '/pages/people-home/people-home?id=' + id,
        })
      })

  },


  navgateTap: function (e) { //点击banner图跳转
    let url = e.currentTarget.dataset.url;
    if (url) {
      wx.navigateTo({
        url: url,
      })
    }
  },

  openProductDetail: function (e) { //打开产品详情
    let that = this;
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/product-detail/product-detail?id=' + id,
    })
  },

  //页面加载
  onLoad: function () {
    this.getCompanyArea(); //协会搜索行业分类
    this.getCompanyList(); //协会搜索公司分类
  },

  onShow: function () {
    this.getIndexData(); //获取首页数据
  },

  getIndexData: function () { //获取首页数据
    var that = this;
    var user_id = wx.getStorageSync('userId')
    app.request(app.globalData.host + 'index/index', {
        user_id
      }, 'GET', false,
      function (res) {
        wx.hideLoading();
        var index = res.data.data;
        that.setData({
          index,
        })
      },
      function (err) {},
      function (complete) {}
    )
  },

  bindPickerAssocType: function (e) { //行业选择
    var that = this;
    var AssTypeIdx = e.detail.value;
    that.setData({
      AssTypeIdx
    })
  },

  bindPickerAssoc: function (e) { //企业选择
    var that = this;
    var AssIdx = e.detail.value;
    that.setData({
      AssIdx
    })
  },

  getCompanyArea: function () { //搜索行业分类
    var that = this;
    wx.request({
      url: app.globalData.host + 'company/getCompanyArea',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'GET',
      success: function (res) {
        wx.setStorageSync('AssocType', res.data.data)
        that.setData({
          AssocType: res.data.data
        })
      },
      fail: function (res) {},
      complete: function (res) {},
    })
  },


  getCompanyList: function () { //搜索企业分类
    var that = this;
    wx.request({
      url: app.globalData.host + 'company/getCompanyList',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'GET',
      success: function (res) {
        that.setData({
          Assoc: res.data.data
        })
      },
      fail: function (res) {},
      complete: function (res) {},
    })
  },
  searchProduct: function () {
    var that = this;
    var {
      AssocType,
      AssTypeIdx,
      Assoc,
      AssIdx
    } = that.data;
    var typeid = AssocType[AssTypeIdx].id;
    var companyid = Assoc[AssIdx].id;
    wx.request({
      url: app.globalData.host + 'index/searchProduct',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        typeid,
        companyid
      },
      method: 'GET',
      success: function (res) {
        that.setData({
          'index.product': res.data.data
        })
      },
      fail: function (res) {},
      complete: function (res) {},
    })
  },
  // 会员服务详情
  servicedetail(e){
    var id=e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/associa-product/associa-product?id='+id,
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showLoading({
      title: '加载中',
    })
    this.data.index = {};
    this.getIndexData(); //获取首页数据
    wx.stopPullDownRefresh()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})